<?php
namespace Common\Model;
use Think\Model;
use Home\Service\ClockTimeService;
use Home\Service\AccountService;
// +----------------------------------------------------------------------
// | Date:2017年3月14日
// +----------------------------------------------------------------------
// | Author: EK_熊<1439527494@qq.com>
// +----------------------------------------------------------------------
// | Description: 此文件作用于****
// +----------------------------------------------------------------------
class ScheduleModel extends Model{
    
    
    
    
    /**
     * 高管行程数据，返回多维数组
     * 参数 ：1上周,2本周,3下周,4整个月
     * date:2017年3月1日
     * author: EK_熊
     */
    public function admin_showlist($weeks,$weeke){
        $cfg = C('SCHEDULE_TYPE');
        
/*          if ($range == 4){
            $weekRange = get_month_range();
        }else{
            $weekRange = get_week_range($range);
        }  */
        
        
//         $weeks = $weekRange['start'];
//         $weeke = $weekRange['end'];
        
        
        $map['date'] = ['between',[$weeks,$weeke]];
        $schedule = $this->where($map)->order('id desc')->group('sn,type,date')->select();
        $cacheTop = F("schedule_top");
        $account = new AccountService();
        $userBox = $account->get_schedule_list();//获取特权是4的数据
        if ($cacheTop) {
            foreach ($userBox as $k=>$v) {
                $snbox[] = $v['sn'];
                if (in_array_case($v['sn'], $cacheTop)){
                    unset($userBox[$k]);
                    $userBox[$v['sn']] = $v;
                }
            }
        }
        foreach ($userBox as $vinfo){
            $snBox[] = $vinfo['sn'];
        }
        $atten = $this->is_check_inout($snBox,$weeks,$weeke);//打卡记录
        
        
        foreach ($userBox as $vinfo){
           $name = $vinfo['ep_name']; 
           $sn = $vinfo['sn']; 
           $ret[$name]['list'] = [];
           $count = ((strtotime($weeke)-strtotime($weeks))/86400)+1;
            for($i=0;$i<=$count;$i++) {
                $day = date('Y-m-d',strtotime($weeks)+$i*86400);
                if (!is_work_day($day)) continue;
        
                $ret[$name]['dep_name'] =$vinfo['dep_name'];
                $ret[$name]['sn'] =$sn;
                $ret[$name]['pos_title'] =$vinfo['pos_title'];
                
                if (!$ret[$name]['list']["type_1"][$i])
                    $ret[$name]['list']["type_1"][$i] = [];
                
                if (!$ret[$name]['list']["type_default"][$i])
                    $ret[$name]['list']["type_default"][$i] = [];
                
                if (!$ret[$name]['list']["type_4"][$i])
                    $ret[$name]['list']["type_4"][$i] = [];
                
                foreach ($schedule as $k=>$v){
                    $type = $v['type'];
                    if ($v['date'] == $day && $v['sn'] == $sn) {
                        if ($v['type'] == 2 || $v['type'] == 3){
                            $ret[$name]['list']["type_default"][$i]=$v;
                        }else{
                            $ret[$name]['list']["type_".$type][$i]=$v;
                        }
                    }
                }
                
                if (isset($atten[$day][$sn])) {
                    $ret[$name]['list']["type_1"][$i]=['type'=>1,'status'=>0];
                }
                
            }

        }
        $ret = array_reverse($ret);
        return $ret;
    }
    
    //获取高管是否在时间范围内是否有打卡数据
    public function is_check_inout($snbox,$weeks,$weeke){
        $map['sn'] = ['in',$snbox];
        $map['date'] = ['between',[$weeks,$weeke]];
        $map['duty_on'] = ['neq'," "]; //请假不算
        $ret = M('AttendanceDay')->where($map)->field('sn,date')->select();
        if ($ret){
            foreach ($ret as $k=>$v){
                
                $data[$v['date']][$v['sn']] = 1;
            }
        }
        
        return $data;
    }
    
    
     public function showlist($range=2){
        $cfg = C('SCHEDULE_TYPE');
        if ($range == 4){
            $weekRange = get_month_range();
        }else{
            $weekRange = get_week_range($range);
        }
        
        
        $weeks = $weekRange['start'];
        $weeke = $weekRange['end'];
        
        
        $map['date'] = ['between',[$weeks,$weeke]];
        $schedule = $this->where($map)->order('id desc')->group('sn,date')->select();
        $cacheTop = F("schedule_top");

        $account = new AccountService();
        $userBox = $account->get_schedule_list();//获取特权是4的数据
        if ($cacheTop) {
            foreach ($userBox as $k=>$v) {
                if (in_array_case($v['sn'], $cacheTop)){
                    unset($userBox[$k]);
                    $userBox[$v['sn']] = $v;
                }
            }
        }
        
        foreach ($userBox as $vinfo){
            $snBox[] = $vinfo['sn'];
        }
        $atten = $this->is_check_inout($snBox,$weeks,$weeke);//打卡记录
        foreach ($userBox as $snk=>$vinfo){
           $name = $vinfo['ep_name'];     
            $sn = $vinfo['sn'];
            for($i=0;$i<=$weekRange['count'];$i++) {
                $day = date('Y-m-d',strtotime($weeks)+$i*86400);
               // if (!is_work_day($day)) continue;

                $ret[$name][$i]=[];
                $ret[$name][$i]['sn']=$sn;
                
                
                foreach ($schedule as $k=>$v){
                    if ($v['date'] == $day && $v['sn'] == $sn) {
                        $ret[$name][$i]=$v;
                        $ret[$name][$i]['type'] =$cfg[$v['type']];
                    }
                }
                
                if (isset($atten[$day][$sn])) {
                    $ret[$name][$i]['type']=$cfg[1];
                }
                
            }

        }
        $ret = array_reverse($ret);
        return $ret;
    } 
    

    
    
    
}